import 'package:pacsun/model/guide_entity.dart';
import 'package:pacsun/theme/app_colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:logger/logger.dart';

import '../../constants/constant.dart';
import '../../utils/PreferenceUtils.dart';

/// 引导dialog
class NewGuideDialog extends StatelessWidget {
  const NewGuideDialog({super.key, required this.index, required this.onTop});

  final int index;
  final VoidCallback onTop;

  @override
  Widget build(BuildContext context) {
    List<GuideEntity> data = GuideEntity.getGuideEntityList();
    switch (index) {
      case 0:
        return _buildGuide1(context, data, onTop);
      case 1:
        return _buildGuide2(context, data, onTop);
      case 2:
        return _buildDialogContent(context, data[2], onPressed: onTop);
      case 3:
        return _buildDialogContent(context, data[3], onPressed: () async {
          bool b = await PreferenceUtils.setBool(
              Constant.SP_KEY_GUIDE_EXECUTED, true);
          Logger().w("数据存储----$b");
          onTop.call();
        });
      default:
        return Container();
    }
  }

  ///  引导1
  Widget _buildGuide1(context, data, onPressed) {
    return Container(
      alignment: Alignment.bottomCenter,
      decoration: BoxDecoration(color: Color.fromRGBO(0, 0, 0, 0.8)),
      child: _buildDialogContent(context, data[0], onPressed: onPressed),
    );
  }

  Widget _buildGuide2(context, data, onPressed) {
    return _buildDialogContent(context, data[1], onPressed: onPressed);

  }


  ///  引导dialog
  Widget _buildDialogContent(BuildContext context, GuideEntity data,
      {required VoidCallback onPressed}) {
    return SizedBox(
      // width: MediaQuery.of(context).size.width * 0.8,
      width: 319.w,
      height: data.type == 1
          ? 159.h
          : data.type == 2
              ? 118.h
              : data.type == 3
                  ? 113.h
                  : 105.h,
      child: Stack(
        fit: StackFit.expand,
        children: [
          SvgPicture.asset(
            data.backgroundResource,
            fit: BoxFit.fill,
          ),
          Positioned.fill(
            child: Container(
              padding: data.type == 1
                  ? EdgeInsets.only(top: 24, left: 16, right: 16).r
                  : EdgeInsets.all(16).r,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text.rich(
                    style: customTextStyle(
                      fontSize: 11,
                    ),
                    TextSpan(
                      children: [
                        TextSpan(
                          text: data.description,
                          style: customTextStyle(
                            fontSize: 16,
                            fontWeight: 500,
                            color: FamilyTextColor.primary,
                            height: 1.6,
                          ),
                        ),
                        if (data.type == 0)
                          TextSpan(
                            text: ' Creator Hub.',
                            style: customTextStyle(
                              fontSize: 16,
                              fontWeight: 600,
                              color: FamilyTextColor.primary,
                              height: 1.4,
                            ),
                          ),
                      ],
                    ),
                    textScaler: TextScaler.noScaling,
                  ),
                  if (data.type == 1) ...[
                    SizedBox(height: 9.h),
                    Text(
                      data.subDescription,
                      style: customTextStyle(
                        fontSize: 16,
                        fontWeight: 500,
                        color: FamilyTextColor.primary,
                        height: 24 / 16,
                      ),
                      textScaler: TextScaler.noScaling,
                    ),
                  ],
                  Visibility(
                      visible: data.type == 1, child: SizedBox(height: 10)),
                  Expanded(child: Container()),
                ],
              ),
            ),
          ),
          Positioned(
            right: 12.w,
            bottom: 8.h,
            child: Row(
              children: [
                GestureDetector(
                  onTap: () {
                    onPressed.call();
                  },
                  child: Container(
                    margin: EdgeInsets.only(bottom: 10),
                    alignment: Alignment.bottomRight,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(25),
                      color: Color.fromRGBO(215, 48, 48, 1),
                    ),
                    padding: EdgeInsets.symmetric(
                        horizontal: data.type == 0 ? 10 : 15, vertical: 3),
                    child: Text(
                      data.buttonText,
                      style: TextStyle(
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w600,
                        color: Color.fromRGBO(255, 255, 255, 1),
                        fontFamily: "Poppins",
                      ),
                      textScaler: TextScaler.noScaling,
                    ),
                  ),
                )
              ],
            ),
          ),
          // 最后一步需要显示
          if (data.type == 3)
            Positioned(
              top: 4.h,
              right: 4.w,
              child: GestureDetector(
                onTap: () {
                  onPressed.call();
                },
                child: Container(
                  width: 24.w,
                  height: 24.h,
                  alignment: Alignment.center,
                  child: SvgPicture.asset(
                    'assets/icons/icon_black_close.svg',
                    width: 15.w,
                    height: 15.h,
                  ),
                ),
              ),
            )
        ],
      ),
    );
  }
}
